package utils

import (
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

// GetResources 获取Pod和ClusterRole资源
func GetResources() error {
	// 加载kubeconfig配置
	kubeconfig := "/path/to/kubeconfig" // 替换为实际的kubeconfig路径
	config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		return fmt.Errorf("failed to load kubeconfig: %v", err)
	}

	// 创建ClientSet
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return fmt.Errorf("failed to create clientset: %v", err)
	}

	// 获取Pod列表
	pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
	if err != nil {
		return fmt.Errorf("failed to get pods: %v", err)
	}
	fmt.Printf("Pods: %+v\n", pods.Items)

	// 获取ClusterRole列表
	clusterRoles, err := clientset.RbacV1().ClusterRoles().List(metav1.ListOptions{})
	if err != nil {
		return fmt.Errorf("failed to get cluster roles: %v", err)
	}
	fmt.Printf("ClusterRoles: %+v\n", clusterRoles.Items)

	return nil
}
